/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package semanticsearch.operation;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeMap;

/**
 *
 * @author Pavithre
 */
public class SearchOperation {

    private boolean doSearch;

    /**
     * Get search results related to the synonyms.
     *
     * @param synonyms
     * @param filePathList
     * @return
     */
    public ArrayList<String> getSearchResultsFromClasses(ArrayList<String> synonyms, ArrayList<String> filePathList) {

        int noOfOccurence = 0;
        String filePath;
        File file;
        Scanner scanner;
        String line = "";
        String[] wordsChunk;
        ArrayList<String> filePaths = new ArrayList<String>();
        TreeMap<Integer, String> map = new TreeMap<Integer, String>();

        doSearch = true;

        // No file paths found
        if (filePathList == null || filePathList.isEmpty()) {

            return null;
        }

        for (int i = 0; i < filePathList.size() && doSearch; i++) {
            
            file = new File(filePathList.get(i));

            try {
                //
                // Create a new Scanner object which will read the data
                // from the file passed in. To check if there are more
                // line to read from it we check by calling the
                // scanner.hasNextLine() method. We then read line one
                // by one till all line is read.
                //
                scanner = new Scanner(file);

                for (int j = 0; scanner.hasNextLine() && doSearch; j++) {
                    
                    noOfOccurence = 0;
                    
                    line = "";
                    for (int k = 0; k <= 3 && scanner.hasNextLine(); k++) {
                        
                        line = line + scanner.nextLine();
                    }
                    
                    wordsChunk = line.split(" ");

                    for (int k = 0; k < wordsChunk.length && doSearch; k++) {

                        for (int l = 0; l < synonyms.size(); l++) {

                            if (wordsChunk[k].equalsIgnoreCase(synonyms.get(l))) {

                                noOfOccurence++;
                            }
                        }
                    }
                    map.put(noOfOccurence, line);
                    System.out.println(line);
                    System.out.println();
                }

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        for (int i = 0; 0 < map.size(); i++) {

            filePaths.add(map.remove(map.lastKey()));
        }

        return filePaths;
    }

    public static void main(String[] ags) {

        ArrayList<String> paths = new ArrayList<String>();
        paths.add("E:\\Search_Material\\text1.txt");
        paths.add("E:\\Search_Material\\text2.txt");
        paths.add("E:\\Search_Material\\text3.txt");
        paths.add("E:\\Search_Material\\text4.txt");
        paths.add("E:\\Search_Material\\text5.txt");
        paths.add("E:\\Search_Material\\text6.txt");

        ArrayList<String> synonyms = new ArrayList<String>();
        synonyms.add("elephants");
        synonyms.add("Elephants");
        synonyms.add("Indian");
        synonyms.add("indian");
        synonyms.add("African");

        SearchOperation so = new SearchOperation();
        ArrayList<String> results = so.getSearchResultsFromClasses(synonyms, paths);

        for (int i = 0; i < results.size(); i++) {
            System.out.println(results.get(i));
        }
    }

    public void stopSearch() {

        doSearch = false;
    }
}
